home *** CD-ROM | disk | FTP | other *** search
/ HaCKeRz Kr0nlcKLeZ 1 / HaCKeRz Kr0nlcKLeZ.iso / chibacity / gbbdisk.arj / POLY / LCG32.ASM < prev    next >
Encoding:
Assembly Source File  |  1995-07-05  |  2.3 KB  |  78 lines

  1. ;32 bit Linear Congruential Pseudo-Random Number Generator
  2.  
  3. .model tiny
  4. .code
  5. .386
  6.  
  7.         PUBLIC  RANDOM_SEED
  8.         PUBLIC  GET_RANDOM
  9.  
  10.  
  11. ;The generator is defined by the equation
  12. ;
  13. ;              X(N+1) = (A*X(N) + C) mod M
  14. ;
  15. ;where the constants are defined as
  16. ;
  17. M               DD      134217729
  18. A               DD      44739244
  19. C               DD      134217727
  20. RAND_SEED       DD      0               ;X0, initialized by RANDOM_SEED
  21.  
  22. ;Set RAND_SEED up with a random number to seed the pseudo-random number
  23. ;generator. This routine should preserve all registers! it must be totally
  24. ;relocatable!
  25. RANDOM_SEED     PROC    NEAR
  26.                 push    si
  27.                 push    ds
  28.                 push    dx
  29.                 push    cx
  30.                 push    bx
  31.                 push    ax
  32.                 call    RS1
  33. RS1:            pop     bx
  34.                 sub     bx,OFFSET RS1
  35.                 xor     ax,ax
  36.                 mov     ds,ax
  37.                 mov     si,46CH
  38.                 lodsd
  39.                 xor     edx,edx
  40.                 mov     ecx,M
  41.                 div     ecx
  42.                 mov     cs:[bx][RAND_SEED],edx
  43.                 pop     ax
  44.                 pop     bx
  45.                 pop     cx
  46.                 pop     dx
  47.                 pop     ds
  48.                 pop     si
  49.                 retn
  50.  
  51. RANDOM_SEED     ENDP
  52.  
  53. ;Create a pseudo-random number and put it in ax.
  54. GET_RANDOM      PROC    NEAR
  55.                 push    bx
  56.                 push    cx
  57.                 push    dx
  58.                 call    GR1
  59. GR1:            pop     bx
  60.                 sub     bx,OFFSET GR1
  61.                 mov     eax,[bx][RAND_SEED]
  62.                 mov     ecx,[bx][A]                         ;multiply
  63.                 mul     ecx
  64.                 add     eax,[bx][C]                         ;add
  65.                 adc     edx,0
  66.                 mov     ecx,[bx][M]
  67.                 div     ecx                                 ;divide
  68.                 mov     eax,edx                             ;remainder in ax
  69.                 mov     [bx][RAND_SEED],eax                 ;and save for next round
  70.                 pop     dx
  71.                 pop     cx
  72.                 pop     bx
  73.                 retn
  74.  
  75. GET_RANDOM      ENDP
  76.  
  77.                 END
  78.